package com.flag.liutao.modules.role.controller;


import com.flag.liutao.common.domain.IdParam;
import com.flag.liutao.common.domain.IdsParam;
import com.flag.liutao.common.domain.LocalUser;
import com.flag.liutao.common.domain.ResponseResult;
import com.flag.liutao.common.enums.MenuTypeEnum;
import com.flag.liutao.modules.role.param.MenuParam;
import com.flag.liutao.modules.role.param.MenuQueryParam;
import com.flag.liutao.modules.role.service.IMenuService;
import com.flag.liutao.modules.role.vo.MenuDTO;
import com.flag.liutao.modules.user.domain.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author godM
 * @since 2022-12-02
 */
@RestController
@RequestMapping("/admin/menu")
@Api(tags = "权限管理")
@Slf4j
@RequiredArgsConstructor
public class MenuController {

    public final IMenuService menuService;

    /**
     * 获取权限列表
     * @param param
     * @return
     */
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys_power:menu_R')")
    @ApiOperation(value="获取权限列表",notes = "权限管理：获取权限列表")
    public ResponseResult<Map<String,Object>> getMenuList(MenuQueryParam param){
        Map<String,Object> map = menuService.getMenuList(param.getPage(),param.getLimit(),param.getType(),param.getKeyword());
        return ResponseResult.ok(map);
    }

    /**
     * 获取用户菜单列表
     * @return
     */
    @GetMapping("user")
    @PreAuthorize("hasAuthority('sys_power:login_admin')")
    @ApiOperation(value = "获取用户权限列表",notes ="权限管理：获取用户权限列表" )
    public ResponseResult<List<MenuDTO>> getUserMenuList(){
        User user = LocalUser.getLocalUser();
        Long id = user.getId();
        List<MenuDTO> menuList = menuService.getUserMenuList(id);
        return ResponseResult.ok(menuList);
    }

    /**
     * 编辑菜单
     * @param param
     * @return
     */
    @PostMapping("edit")
    @PreAuthorize("hasAuthority('sys_power:menu_U')")
    @ApiOperation(value = "修改权限信息",notes = "权限管理：修改权限信息")
    public ResponseResult<Boolean> editMenu(@Validated @RequestBody MenuParam param){
        menuService.updateMenu(param);
        return ResponseResult.ok();
    }

    /**
     * 新增权限
     * @param param
     * @return
     */
    @ApiOperation(value = "新增权限",notes = "权限管理：新增权限")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys_power:menu_C')")
    public ResponseResult<Boolean> addMenu(@Validated @RequestBody MenuParam param){
        menuService.addMenu(param);
        return ResponseResult.ok();
    }

    /**
     * 删除权限
     * @param param
     * @return
     */
    @ApiOperation(value = "删除权限",notes = "权限管理：删除权限")
    @PostMapping("/del")
    @PreAuthorize("hasAuthority('sys_power:menu_D')")
    public ResponseResult<Boolean> delMenu(@Validated @RequestBody IdParam param){
        menuService.delMenu(param.getId());
        return ResponseResult.ok();
    }

    /**
     * 批量删除权限
     * @param param
     * @return
     */
    @ApiOperation(value = "批量删除权限",notes = "权限管理：批量删除权限")
    @PostMapping("/dels")
    @PreAuthorize("hasAuthority('sys_power:menu_D')")
    public ResponseResult<Boolean> delsMenu(@Validated @RequestBody IdsParam param){
        List<Long> ids = param.getIds();
        for (Long id : ids) {
            menuService.delMenu(id);
        }
        return ResponseResult.ok();
    }

    /**
     * 获取所有权限
     * @return
     */
    @GetMapping("/menuList")
    @PreAuthorize("hasAuthority('sys_power:menu_R')")
    @ApiOperation(value="获取权限列表",notes = "权限管理：获取权限列表")
    public ResponseResult<Map> getMenuList(){
        Map<String,Object> map = menuService.getMenuList();
        return ResponseResult.ok(map);
    }
}
